home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 020 / pack / pack.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  7KB  |  232 lines

  1. /**********************************************************/
  2. /*                                                        */
  3. /* This program accepts a list of C source or header files*/
  4. /* and filters out all the comments and unneccesary white */
  5. /* space characters--thereby, reducing the overall file   */
  6. /* size and increasing the amount of user usable disk     */
  7. /* space.                                                 */
  8. /*                                                        */
  9. /* Written by, Jeff S. Dickson, April 9, 1986             */
  10. /* Feel free to distribute it to anyone who is interested */
  11. /* in reclaiming desperately needed disk space.           */
  12. /**********************************************************/
  13.  
  14.  
  15. #include <stdio.h>
  16. #include "wildexp.h"
  17.  
  18. #define NULFILPNTR   ((FILE *)0)
  19.  
  20. #define MAXPREFIXLEN 32
  21.  
  22. main(argc, argv)
  23.  
  24. int argc;
  25. char *argv[];
  26. {
  27.  
  28.     char filename[128];
  29.     char pathname[128];
  30.     char prepend_str[128];
  31.     char temp[128];
  32.  
  33.     int c;
  34.     int i;
  35.     int j;
  36.     int len;
  37.     int newlen;
  38.     int maxitems;
  39.     int Slash = FALSE;
  40.     int Asterik = FALSE;
  41.     int Comment = FALSE;
  42.     int WhiteSpace = FALSE;
  43.     int NewLine = FALSE;
  44.     int CharsRead;
  45.     int CharsWritten;
  46.     int Chars2rewrite;
  47.     int TotalChars = 0;
  48.  
  49.     FILE *sfp, *tfp, *lfp;
  50.  
  51.  
  52.     /* verify that the correct number of args were specified */
  53.  
  54.     if (argc == 1) {
  55.        printf("Insufficient Arguments\n\n");
  56.        printf("Usage: pack [-p<pathname prefix string>] <file spec> [etc...]\n");
  57.        exit();
  58.     }
  59.  
  60.     /* open the scratch files */
  61.  
  62.     if ((lfp = fopen("ram:listfile", "w+")) == NULFILPNTR) {
  63.        printf("pack: Can't create list file\n");
  64.        exit();
  65.     }
  66.     if ((tfp = fopen("ram:tempfile", "w+")) == NULFILPNTR) {
  67.        printf("pack: Can't create sratch file\n");
  68.        fclose(lfp);
  69.        unlink("ram:listfile");
  70.        exit();
  71.     }
  72.  
  73.     /* check if the user specifed a directory specification */
  74.     /* to be prepended to filenames                         */
  75.  
  76.     if (strncmp(*(argv + 1), "-p", 2) == 0) {
  77.        strncpy(prepend_str, *++argv + 2, MAXPREFIXLEN);
  78.        --argc;
  79.     }
  80.     else
  81.        prepend_str[0] = '\0';
  82.     len = strlen(prepend_str);
  83.  
  84.     /* main loop -- take the current arg, generate a list of */
  85.     /* files that could possibly correspond to it, and cond- */
  86.     /* ence them                                             */
  87.  
  88.     for (; argc != 1; argc--) {
  89.        prepend_str[len] = '\0';
  90.        strncpy(temp, *++argv, sizeof (temp));
  91.        for (i = j = 0; temp[i] != '\0'; i++)
  92.            if (temp[i] == '/' || temp[i] == ':')
  93.                j = i + 1;
  94.  
  95.        if ((newlen = len + j) > sizeof (prepend_str)) {
  96.            printf("Can't do %s - argument too long\n",temp);
  97.            continue;
  98.        }
  99.        strncat(prepend_str, temp, j);
  100.        strcpy(filename, prepend_str);
  101.        if (strlen(&temp[j]) + newlen > sizeof (filename)) {
  102.            printf("Can't do %s - argument too long\n",temp);
  103.            continue;
  104.        }
  105.        strcat(filename, &temp[j]);
  106.  
  107.        rewind(lfp);
  108.        maxitems = makelist(filename, lfp);
  109.        rewind(lfp);
  110.  
  111.        while ((fgets(filename, sizeof (filename) - newlen,
  112.                lfp) != (char *)0) && (maxitems-- != 0)) {
  113.           filename[strlen(filename) - 1] = '\0';
  114.           strcpy(pathname, prepend_str);
  115.           strcat(pathname, filename);
  116.           if ((sfp = fopen(pathname, "r+")) == NULFILPNTR) {
  117.              printf("pack: Can't open %s\n", pathname);
  118.              continue;
  119.           }
  120.           rewind(tfp);
  121.           printf("Condensing %s ====> ", pathname);
  122.           CharsRead = 0;
  123.           CharsWritten = 0;
  124.           while ((c = getc(sfp)) != EOF) {
  125.              ++CharsRead;
  126.              if (!(Comment)) {
  127.                 switch(c) {
  128.                    case '/' : if (Slash) {
  129.                                  ungetc(c, sfp);
  130.                                  Slash = FALSE;
  131.                                  break;
  132.                               }
  133.                               Slash = TRUE;
  134.                               continue;
  135.                    case '*' : if (Slash) Comment = TRUE;
  136.                               else break;
  137.                               continue;      
  138.                    case ' ' :
  139.                    case '\t': if (!(WhiteSpace)) {
  140.                                  WhiteSpace = TRUE;
  141.                                  break;
  142.                               }
  143.                               continue;
  144.                    case '\n': if (!(Slash)) {
  145.                                  if (!(NewLine)) {
  146.                                     NewLine = TRUE;
  147.                                     WhiteSpace = TRUE;
  148.                                     break;
  149.                                  }
  150.                                  else continue;
  151.                               }
  152.                    default  : if (Slash) {
  153.                                  ungetc(c, sfp);
  154.                                  c = '/';
  155.                               }
  156.                               NewLine = FALSE;
  157.                               WhiteSpace = FALSE;
  158.                               Slash = FALSE;
  159.                 }
  160.                 putc(c, tfp);
  161.                 ++CharsWritten;
  162.              }
  163.              else
  164.              {
  165.                 switch(c) {
  166.                    case '/' : if (Asterik) {
  167.                                  Comment = FALSE;
  168.                                  Slash = FALSE;
  169.                                  Asterik = FALSE;
  170.                               }
  171.                               break;
  172.                    case '*' : Asterik = TRUE;
  173.                               break;
  174.                    default  : Asterik = FALSE;
  175.                 }
  176.              }
  177.           }                     
  178.           if ((sfp = freopen(pathname,"w", sfp)) == NULFILPNTR)
  179.              printf("pack: Error Re-opening %s for rewrite\n", pathname);
  180.           else
  181.           {
  182.              rewind(tfp);
  183.              for (Chars2rewrite = 0; Chars2rewrite < CharsWritten; Chars2rewrite++) {
  184.                  if ((c = getc(tfp)) == EOF)
  185.                     break;
  186.                  putc(c, sfp);
  187.              }
  188.              printf("\rThere are now %6d less characters in %s\n",(CharsRead - CharsWritten), pathname);
  189.              TotalChars += (CharsRead - CharsWritten);
  190.           }
  191.           fclose(sfp);
  192.        }
  193.     }
  194.     printf("\nTotal character(s) removed %d\n", TotalChars);
  195.     fclose(tfp);
  196.     fclose(lfp);
  197.     unlink("ram:tempfile");
  198.     unlink("ram:listfile");
  199. }
  200.  
  201. /* this function makes use of the routines written by Rick */
  202. /* Schaeffer [CPS 70120,124] to construct a list of all    */
  203. /* possible files that correspond to the passed filename   */
  204.  
  205. int makelist(pattern, list)
  206.  
  207. char *pattern;
  208. FILE *list;
  209. {
  210.    int retval;
  211.    int NumberOfItems= 0;
  212.    int search4first = TRUE;
  213.  
  214.    struct find fwk;
  215.  
  216.    for (;;) {
  217.       if (search4first) {
  218.          if ((retval = findfirst(pattern, &fwk)) != 0)
  219.             break;
  220.       }
  221.       else if ((retval = findnext(&fwk)) < 0)
  222.          break;
  223.  
  224.       fputs(fwk.fname, list);
  225.       fputc('\n', list);
  226.       ++NumberOfItems;
  227.       search4first = FALSE;
  228.    }
  229.    find_cleanup(&fwk);
  230.    return(NumberOfItems);
  231. }
  232.